<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="体系名称" prop="systemName">
        <el-input
          v-model="queryParams.systemName"
          placeholder="请输入体系名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	     <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['standardsystem:standardsystem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    
    <el-row>  
      <el-col :span="8">
        <el-table
          v-if="refreshTable"
          v-loading="loading"
          :data="standardsystemList"
          row-key="id"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column label="体系名称" prop="systemName" />
          <el-table-column label="体系图片" align="center" prop="systemImg" width="100">
            <template slot-scope="scope">
              <image-preview :src="scope.row.systemImg" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleAddItem(scope.row)"
              >新增标准</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="listStandard(scope.row)"
              >管理</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['standardsystem:standardsystem:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"
                v-hasPermi="['standardsystem:standardsystem:add']"
              >新增</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['standardsystem:standardsystem:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col style="margin-left:20px;" :span="14">
        <el-table v-loading="loadingItem" :data="itemList">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="标准号" align="center" prop="standardNum" />
          <el-table-column label="标准名称" align="center" prop="standardName" />
          <el-table-column label="标准状态" align="center" prop="status" />
          <el-table-column label="发布时间" align="center" prop="efd" />
          <el-table-column label="实施时间" align="center" prop="isd" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDeleteItem(scope.row)"
                v-hasPermi="['system:item:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="itemQueryParams.pageNum"
          :limit.sync="itemQueryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    
    <!-- 添加或修改体系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="体系名称" prop="systemName">
          <el-input v-model="form.systemName" placeholder="请输入体系名称" />
        </el-form-item>
        <el-form-item label="体系图片" prop="systemImg">
          <image-upload v-model="form.systemImg"/>
        </el-form-item>
        <el-form-item label="父节点" prop="parentId">
          <treeselect v-model="form.parentId" :options="standardsystemOptions" :normalizer="normalizer" placeholder="请选择${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加体系标准对话框 -->
    <el-dialog :title="title" :visible.sync="openitem" width="800px" append-to-body>

        <el-form :model="queryParamsStandard" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="标准号" prop="systemName">
            <el-input
              v-model="queryParamsStandard.a100"
              placeholder="请输入体系名称"
              clearable
              @keyup.enter.native="handleStandardQuery"
            />
          </el-form-item>
          <el-form-item label="标准名称" prop="createDate">
            <el-input
              v-model="queryParamsStandard.a298"
              placeholder="请输入标准名称"
              clearable
              @keyup.enter.native="handleStandardQuery"
            />
          </el-form-item>
          <el-form-item> 
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleStandardQuery">搜索</el-button>
            <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetStandardQuery">重置</el-button> -->
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loadingItemtable"
          :data="db15List"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="标准号" align="center" prop="a100" >
            <template slot-scope="scope">
              <div v-html="scope.row.a100"></div>
            </template>
          </el-table-column>
          <el-table-column label="中文标准名称" align="center" prop="a298" >
            <template slot-scope="scope">
              <div v-html="scope.row.a298"></div>
            </template>
          </el-table-column>
          <el-table-column label="标准状态" align="center" prop="a298" >
            <template slot-scope="scope">
              <div v-html="scope.row.a200"></div>
            </template>
          </el-table-column>
          <el-table-column label="发布日期" align="center" prop="a298" >
            <template slot-scope="scope">
              <div v-html="scope.row.a101"></div>
            </template>
          </el-table-column>
          <el-table-column label="实施日期" align="center" prop="a298" >
            <template slot-scope="scope">
              <div v-html="scope.row.a205"></div>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="totalStandard > 0"
          :total="totalStandard"
          :page.sync="queryParamsStandard.pageNum"
          :limit.sync="queryParamsStandard.pageSize"
          @pagination="getListStandard"
        />
        <el-button type="primary" @click="addSystemItem">添 加</el-button>
    </el-dialog>

  </div>
</template>

<script>
import { listStandardsystem, getStandardsystem, delStandardSystem, addStandardsystem, updateStandardSystem } from "@/api/system/standardsystem";
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/system/systemstandarditem";
import {
  listDb15,
  getDb15,
  delDb15,
  addDb15,
  updateDb15,
  listStandard,
  getStandard,
  updateStandard
} from "@/api/system/db15";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Standardsystem",
  components: {
    Treeselect
  },
  data() {
    return {
      //选择遮罩层
      loadingItemtable:false,
      //子集遮罩层
      loadingItem: false,
      // 体系子表格数据
      itemList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 体系表格数据
      standardsystemList: [],
      // 体系树选项
      standardsystemOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // Standard表格数据
      db15List: [],
      // 查询参数
      queryParams: {
        systemName: null,
        systemImg: null,
        parentId: null,
        category: 0
      },
      //增加子体系标准
      openitem: false,
      // 总条数
      total: 0,
      totalStandard: 0,
      queryParamsStandard: {
        pageNum: 1,
        pageSize: 10
      },
      //子项查询参数
      itemQueryParams: {
        pageNum: 1,
        pageSize: 10,
        systemId: null,
        standardId: null,
        standardName: null,
        standardNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 选中数组
      ids: [],
      // 选中标准
      standardList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选择体系id
      chosesystemid: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询体系列表 */
    getList() {
      this.loading = true;
      listStandardsystem(this.queryParams).then(response => {
        this.standardsystemList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /* 获取标准数据 */
    getListStandard(){
      this.loadingItemtable = true;
      listStandard(this.queryParamsStandard).then((response) => {
        this.db15List = response.rows;
        this.totalStandard = response.total;
        this.loadingItemtable = false;
      });
    },
    /** 转换体系数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.systemName,
        children: node.children
      };
    },
	/** 查询体系下拉树结构 */
    getTreeselect() {
      listStandardsystem().then(response => {
        this.standardsystemOptions = [];
        const data = { id: 0, systemName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.standardsystemOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        systemName: null,
        systemImg: null,
        createTime: null,
        parentId: null,
        category: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /*搜索标准 */
    handleStandardQuery(){
        this.loadingItemtable = true;
      listStandard(this.queryParamsStandard).then((response)=>{
        this.db15List = response.rows;
        this.totalStandard = response.total;
        this.loadingItemtable = false;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加体系";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getStandardsystem(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改体系";
      });
    },
    /*查询子标准*/ 
    listStandard(row){
      this.loadingItem = true;
      this.itemQueryParams.systemId =  row.id;
      listItem(this.itemQueryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loadingItem = false;
      });
    },
    /*新增子标准*/
    handleAddItem(row){
      this.chosesystemid = row.id
      this.openitem=true;
      // this.loadingItemtable = true;
      // listDb15(this.queryParams).then((response) => {
      //   this.db15List = response.rows;
      //   this.totalStandard = response.total;
      //   this.loadingItemtable = false;
      // });
    },
    handleSelectionChange(selection){
      this.ids = selection.map((item) => item.id);
      this.standardList = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 添加子标准体系
    addSystemItem(){
      let sysid = this.chosesystemid
      this.standardList.forEach(function(item, index){
        let obju = {
          systemId:sysid,
          standardId:item.id,
          standardName:item.a298,
          standardNum:item.a100,
          status:item.a200,
          efd:item.a101,
          isd:item.a205
        }
        console.log(obju)
        addItem(obju).then(response => {          
          
        });
      })
      this.openitem = false;
      this.getListItem();
    },
    getListItem(){
      listItem(this.itemQueryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loadingItem = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.category = 0
            updateStandardSystem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.category = 0
            addStandardsystem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除体系编号为"' + row.id + '"的数据项？').then(function() {
        return delStandardSystem(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /* 删除子项 */
    handleDeleteItem(row){
      this.$modal.confirm('是否确认删除子项标准编号"' + row.id + '"的数据项？').then(function() {
        return delItem(row.id);
      }).then(() => {
        this.getListItem();
      }).catch(() => {});
    }
  }
};
</script>
